package com.lzy.crm.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lzy.crm.base.mybatis.BaseMapper;
import com.lzy.crm.base.mybatis.Example;
import com.lzy.crm.base.service.BaseServiceImpl;
import com.lzy.crm.dao.CrowdApplyMapper;
import com.lzy.crm.dao.CrowdMapper;
import com.lzy.crm.dao.criteria.CrowdApplyCriteria;
import com.lzy.crm.domain.Crowd;
import com.lzy.crm.domain.CrowdApply;
import com.lzy.crm.domain.PpUser;
import com.lzy.crm.service.CrowdApplyService;
import com.lzy.crm.service.CrowdUserService;
import com.lzy.crm.service.UserService;
import com.lzy.crm.vo.PlayerInfo;

@Service
public class CrowdApplyServiceImpl  extends BaseServiceImpl<CrowdApply, CrowdApplyCriteria> implements CrowdApplyService{

	@Resource
	private CrowdApplyMapper crowdApplyMapper;
	
	
	@Resource
	private CrowdMapper crowdMapper;
	
	@Resource
	private UserService userService;
	
	@Resource
	private CrowdUserService crowdUserService;
	
	@Override
	public BaseMapper<CrowdApply, CrowdApplyCriteria> getBaseMapper() {
		return crowdApplyMapper;
	}

	
	
	/**
	 *  申请加入群方法
	 * @param crowdId
	 * @param remark
	 */
	@Override
	public int applyCrowd(int crowdId,String remark,PpUser pUser){
		Crowd crowd = crowdMapper.getById(crowdId);
		if(crowd==null){
			return 0;
		}
		if(checkCrowdApply(String.valueOf(crowdId),String.valueOf(pUser.getUid()))){
			// 已经申请过了
			return 2;
			
		}
		// 群主信息
		CrowdApply crowdApply =new CrowdApply();
		crowdApply.setCrowdId(crowd.getId());
		crowdApply.setCrowdName(crowd.getCrowdName());
		crowdApply.setCrowdGameId(crowd.getGameId());
		crowdApply.setCrowdUnionid(crowd.getUnionid());
		crowdApply.setCrowdNickName(pUser.getNickname());
		crowdApply.setCrowdStatus("0");
		crowdApply.setCrowdRemark("");
		crowdApply.setGameId(pUser.getUid());
		crowdApply.setGameUnionid(pUser.getUnionid());
		crowdApply.setGameNickName(pUser.getNickname());
		crowdApply.setGameStatus("1");
		crowdApply.setGameRemark(remark);
		crowdApply.setType("2");
		crowdApply.setStatus("0");
		crowdApply.setCreateTime(new Date());
		crowdApplyMapper.insertSelective(crowdApply);
		return 1;
	}
	
	/**
	 * 检查是否已经申请过了
	 * @param crowdId
	 * @param gameId
	 * @return
	 */
	public boolean checkCrowdApply(String crowdId,String gameId){
		Example<CrowdApplyCriteria> example = new Example<CrowdApplyCriteria>(CrowdApplyCriteria.class);
		CrowdApplyCriteria crowdApplyCriteria = example.createCriteria();
		crowdApplyCriteria.andCrowdIdEqualTo(crowdId);
		crowdApplyCriteria.andGameIdEqualTo(gameId);
		crowdApplyCriteria.andStatusEqualTo("0");
		crowdApplyCriteria.andCrowdStatusEqualTo("0");
		List<CrowdApply> crowdApplyList = crowdApplyMapper.getListByExample(example);
		return crowdApplyList.size()>0?true:false;
		
	}
	
	/**
	 * 查询群申请信息
	 * @param gameId
	 * @return
	 */
	@Override
	public List<CrowdApply> getCrowdApplyListByGameId(String gameId){
		return crowdApplyMapper.getCrowdApplyListByGameId(gameId);
	}

	/**
	 * 我的申请信息
	 * @param gameId
	 * @return
	 */
	public List<CrowdApply> getMyCrowdApplyListByGameId(String gameId){
		return crowdApplyMapper.getMyCrowdApplyListByGameId(gameId);
	}
	
	
	/**
	 * 修改申请信息
	 * @param crowdApplyId
	 * @param status
	 * @param remark
	 * @return
	 * @throws Exception 
	 */
	@Override
	@Transactional
	public int updateCrowdApply(String crowdApplyId,String status,String remark,PpUser pUser) throws Exception{
		CrowdApply crowdApply = crowdApplyMapper.getById(crowdApplyId);
		if(status.equals("3")){
			crowdApply.setStatus("1");
		}else if(status.equals("2")){
			crowdApply.setCrowdStatus(status);
			crowdApply.setCrowdRemark(remark);
		}else if(status.equals("1")){
			crowdApply.setCrowdStatus(status);
			crowdApply.setCrowdRemark(remark);
			
			crowdUserService.addCrowdUser(crowdApply, remark);
		}
		crowdApply.setUpdateTime(new Date());
		crowdApplyMapper.updateByPrimaryKeySelective(crowdApply);
		return 1;
	}
	
	/**
	 *  查询群申请的消息数量
	 * @param crowdId
	 * @param remark
	 */
	public int getApplyList(int crowdId ,String stauts){
		return crowdApplyMapper.getApplyList(crowdId,stauts);
	}
	
	
}
